package com.yami.shop.admin.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.User;
import com.yami.shop.bean.model.UserAddr;
import com.yami.shop.bean.model.UserFundDetail;
import com.yami.shop.bean.vo.UserFundDetailVo;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.admin.util.SecurityUtils;
import com.yami.shop.service.UserAddrService;
import com.yami.shop.service.UserFundDetailService;
import com.yami.shop.service.UserService;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户资金详情 前端控制器
 * </p>
 *
 * @author jcd
 * @since 2024-07-25
 */
@RestController
@AllArgsConstructor
@RequestMapping("/user/fundDetail")
public class UserFundDetailController {
    private final UserFundDetailService userFundDetailService;

    @Autowired
    private UserService userService;

    /**
     * 分页查询
     *
     * @param page     分页对象
     * @param fundDetail 用户地址管理
     * @return 分页数据
     */
    @PreAuthorize("@pms.hasPermission('user:fundDetail:page')")
    @GetMapping("/page")
    public ServerResponseEntity<IPage<UserFundDetail>> getUserAddrPage(PageParam page, UserFundDetailVo fundDetail) {
        LambdaQueryWrapper<UserFundDetail> userFundDetailLambdaQueryWrapper = new LambdaQueryWrapper<>(fundDetail);

        userFundDetailLambdaQueryWrapper.exists(StringUtils.isNotBlank(fundDetail.getUserName()),"(select null from tz_user u where u.user_id = e_user_fund_detail.uid and u.nick_name like '%"+fundDetail.getUserName()+"%')");

        userFundDetailLambdaQueryWrapper.orderByDesc(UserFundDetail::getCreateTime);
        PageParam page1 = userFundDetailService.page(page,userFundDetailLambdaQueryWrapper);

        List<UserFundDetail> records = page1.getRecords();

        List<UserFundDetailVo> list1=new ArrayList<>();
        for (UserFundDetail detail:records){
            UserFundDetailVo vo=new UserFundDetailVo();
            BeanUtil.copyProperties(detail,vo);

            User byId = userService.getById(detail.getUid());
            if (byId != null){
                vo.setUserName(byId.getNickName());
            }
            list1.add(vo);
        }

        page1.setRecords(list1);
        return ServerResponseEntity.success(page1);
    }


    /**
     * 通过id查询用户地址管理
     *
     * @param id id
     * @return 单个数据
     */
    @PreAuthorize("@pms.hasPermission('user:fundDetail:info')")
    @GetMapping("/info/{id}")
    public ServerResponseEntity<UserFundDetail> getById(@PathVariable("id") Long id) {
        return ServerResponseEntity.success(userFundDetailService.getById(id));
    }

    /**
     * 新增用户地址管理
     *
     * @param userAddr 用户地址管理
     * @return 是否新增成功
     */
    @SysLog("新增用户地址管理")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('user:fundDetail:save')")
    public ServerResponseEntity<Boolean> save(@RequestBody @Valid UserFundDetail fundDetail) {
        Long userId = SecurityUtils.getSysUser().getUserId();

        fundDetail.setCreateTime(new Date());
        fundDetail.setCreateBy(userId);
        fundDetail.setDelFlag(0);

        return ServerResponseEntity.success(userFundDetailService.save(fundDetail));
    }

    /**
     * 修改用户地址管理
     *
     * @param userAddr 用户地址管理
     * @return 是否修改成功
     */
    @SysLog("修改用户地址管理")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('user:fundDetail:update')")
    public ServerResponseEntity<Boolean> updateById(@RequestBody @Valid UserFundDetail fundDetail) {
        Long userId = SecurityUtils.getSysUser().getUserId();

        fundDetail.setUpdateBy(userId);
        fundDetail.setUpdateTime(new Date());

        return ServerResponseEntity.success(userFundDetailService.updateById(fundDetail));
    }

    /**
     * 通过id删除用户地址管理
     *
     * @param id id
     * @return 是否删除成功
     */
    @SysLog("删除用户地址管理")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('user:fundDetail:delete')")
    public ServerResponseEntity<Boolean> removeById(@PathVariable("id") Long id) {
        return ServerResponseEntity.success(userFundDetailService.removeById(id));
    }
}

